/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.enac.geoapp.gui.component;

import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;

import edu.enac.geoapp.gui.custom.component.ImageButton;
import edu.enac.geoapp.gui.listener.OrientationListener;
import edu.enac.geoapp.model.Orientation;

/**
 * 
 * @author mathieu
 *	Component containing two button to select the orientation of the map
 *	NOT FULLY IMPLEMENTED
 */
@SuppressWarnings("serial")
public class OrientationSelector extends JComponent {
	private final ImageButton north_button;
	private final ImageButton heading_button;
	private List<OrientationListener>  listeners;
	
	public OrientationSelector(Orientation initOrientation) {
		listeners = new ArrayList<OrientationListener>();
		
		north_button = new ImageButton("images/green_north.png",
				"images/black_north.png");
		heading_button = new ImageButton("images/green_heading.png",
				"images/black_heading.png");
		setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
		
		add(north_button);
		add(heading_button);
		
		if(initOrientation.equals(Orientation.HEADING)) {
			heading_button.setActive(true);
			north_button.setActive(false);
		} else {
			north_button.setActive(true);
			heading_button.setActive(false);
		}
		
		north_button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				north_button.setActive(true);
				heading_button.setActive(false);
				alertOrientationListener(Orientation.NORTH);
			}
		});
		
		heading_button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				heading_button.setActive(true);
				north_button.setActive(false);
				alertOrientationListener(Orientation.HEADING);
			}
		});
		
	}
	
	public void addOrientationListener(OrientationListener o) {
		listeners.add(o);
	}
	
	public void removeListener(OrientationListener o) {
		listeners.remove(o);
	}
	
	private void alertOrientationListener(Orientation newOrientation) {
		for(OrientationListener l: listeners) 
			l.orientationChanged(newOrientation);
	}
	
	
}
